package com.qp.basics.admin.controller;

import com.qp.basics.common.domain.UserInfo;
import com.qp.basics.common.model.R;
import com.qp.basics.common.repository.BaseRepository;
import com.qp.basics.common.utils.RequestHolder;
import com.qp.basics.common.utils.StringUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class BaseController
{
    protected String msg;
    
    protected int page = 1;
    
    protected int rows = 15;
    
    @Value("${attachmentRoot}")
    public String attachmentRoot;
    @Autowired
    private BaseRepository<UserInfo, Long> userInfoRepository;

    public UserInfo getCurrentUserInfox() {
        String userInfoId = getRequest().getHeader("auth-user-id");
        System.out.println("head userInfoId ="+userInfoId);
        if(!StringUtils.isEmpty(userInfoId) && !"undefined".equals(userInfoId)){
            UserInfo userInfo = userInfoRepository.get(Long.parseLong(userInfoId),UserInfo.class);
            return userInfo;
        }
        String wxopenid = getRequest().getHeader("wxopenid");
        if(!StringUtils.isEmpty(wxopenid) && !"undefined".equals(wxopenid)){
            UserInfo userInfo = userInfoRepository.queryUniqueResult("from UserInfo where openId='"+wxopenid+"'", null);
            return userInfo;
        }
        return null;
    }
    public HttpSession getSession()
    {
        return RequestHolder.getHttpServletRequest().getSession();
    }
    
    public HttpServletRequest getRequest()
    {
        return RequestHolder.getHttpServletRequest();
    }

    public final static String[] movePicTypeStrings = {"bmp", "gif", "jpeg", "jpg", "png", "small", "middle"};

    /**
     * 成功操作
     * */
    public ResponseEntity success()
    {
        return new ResponseEntity<>(HttpStatus.OK);
    }
    
    /**
     * 成功操作
     * */
    public ResponseEntity success(String msg)
    {
        R r = R.ok(msg);
        return new ResponseEntity<>(r,HttpStatus.OK);
    }
    
    /**
     * 成功操作
     * */
    public ResponseEntity success(Object data)
    {
        R r = R.ok(data);
        return new ResponseEntity<>(r,HttpStatus.OK);
    }
    
    /**
     * 成功操作
     * */
    public ResponseEntity success(int code, Object data)
    {
        R r = R.restResult(data,code,(String)null);
        return new ResponseEntity<>(r,HttpStatus.OK);
    }
    /**
     * 失败操作
     * */
    public ResponseEntity failure(String msg)
    {
        R r = R.fail(msg);
        return new ResponseEntity<>(r,HttpStatus.INTERNAL_SERVER_ERROR);
    }
    
    /**
     * 页面跳转
     * */
    public ModelAndView JumpPage(String path)
    {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(path);
        return modelAndView;
    }
    
    /**
     * 带参数的页面跳转
     * */
    public ModelAndView JumpPage(String path, Map<String, ?> params)
    {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(path);
        modelAndView.addAllObjects(params);
        return modelAndView;
    }

    
    protected static String[] getNullPropertyNames(Object source)
    {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
        
        Set<String> emptyNames = new HashSet<String>();
        for (java.beans.PropertyDescriptor pd : pds)
        {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null)
                emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }
    
    protected void responseWrite(String str)
    {
        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        HttpServletResponse response = attributes.getResponse();
        
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        
        try
        {
            response.getWriter().write(str);
        }
        catch (IOException var3)
        {
            throw new RuntimeException(var3.getMessage(), var3);
        }
    }
}
